package edu.osu.intelligentagents.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

class SBRD {
	public float reward;
	public float p;

	public SBRD(String line) {
		reward = Float.valueOf(line.split(",")[0]);
		p = Float.valueOf(line.split(",")[1]);
	}

	@Override
	public String toString() {
		return "(" + reward + "," + p + ")";
	}
}

public class Bandit {

	public Bandit() {
		// TODO Auto-generated constructor stub
	}

	public Bandit(String fileName) {
		try {
			BufferedReader bufferedReader = new BufferedReader(new FileReader(
					new File(fileName)));
			String line = bufferedReader.readLine();
			arms = new Vector<>();
			String armName = null;
			Vector<SBRD> sbrds = null;
			while (line != null) {
				if (line.startsWith("#")) {
					if (armName != null) {
						arms.add(new Arm(armName, sbrds));
					}
					sbrds = new Vector<>();
					armName = line.replace("#", "");
				} else {
					sbrds.add(new SBRD(line));
				}
				line = bufferedReader.readLine();
			}
			arms.add(new Arm(armName, sbrds));
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Vector<Arm> arms;

	public void setArms(Vector<Arm> arms) {
		this.arms = arms;
	}

	public Vector<Arm> getArms() {
		return arms;
	}

	public float pullArm(int armId) {
		return arms.get(armId).getReward();
	}
}
